package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.constant.SystemRoleConstant;
import com.xueyi.aixihu.entity.po.DtNewsSort;
import com.xueyi.aixihu.entity.vo.ComplainRecordVo;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.ComplaintMapper;
import com.xueyi.aixihu.reqeust.ComplaintReq;
import com.xueyi.aixihu.reqeust.SelectComplaintReq;
import com.xueyi.aixihu.service.ComplaintService;
import com.xueyi.aixihu.status.SystemCode;
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ComplaintServiceImpl implements ComplaintService {
    @Autowired
    private ComplaintMapper complaintMapper;
    @Override
    public List<ComplainRecordVo> getList(SelectComplaintReq req) throws SzException {
        req.format();
        Integer roleId = DtUsersVo.findCurrentUser().getUserMaxRole();
        if (!SystemRoleConstant.JW.equals(roleId)){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"没有权限");
        }
        List<ComplainRecordVo> list=complaintMapper.list(req.getCurrentStatus(),req.getKeyWord(),req.getAreaId(),req.getStartIndex(),req.getLimit());
        return list;
    }

    @Override
    public Map<String, Integer> statisticsTotalNumber(SelectComplaintReq req) throws SzException {
        req.format();
        Integer roleId = DtUsersVo.findCurrentUser().getUserMaxRole();
        if (!SystemRoleConstant.JW.equals(roleId)){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"没有权限");
        }
        Map<String, Integer> stringIntegerMap = complaintMapper.statisticsTotalNumber(req.getKeyWord(), req.getAreaId());
        return stringIntegerMap;
    }

    @Override
    public List<DtNewsSort> selectAllAreaInfoList() throws SzException {
        Integer roleId = DtUsersVo.findCurrentUser().getUserMaxRole();
        if (!SystemRoleConstant.JW.equals(roleId)){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"没有权限");
        }
        return complaintMapper.selectAllAreaInfoList();
    }


    @Override
    public List<ComplaintReq> deal(ComplaintReq complaintReq) throws SzException {
        complaintReq.format();
        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();
        if (!SystemRoleConstant.JW.equals(role)){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"没有权限");
        }
        complaintMapper.deal(complaintReq.getId(),1,new Date(),complaintReq.getProcessRemark());
        return null;
    }

    @Override
    public List<ComplaintReq> add(ComplaintReq complaintReq) throws SzException {
        complaintReq.format();
        if (complaintReq.getComplatinTitle()==null){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"主题不能为空");
        }
        if (complaintReq.getAddressId()==null){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"地址不能为空");
        }
        if (complaintReq.getRemark()==null){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"回访内容不能为空");
        }
        complaintMapper.add(complaintReq.getNikeName(),complaintReq.getUserPhone(),complaintReq.getComplatinTitle(),
                complaintReq.getAddressId(),complaintReq.getRemark(),complaintReq.getRemarkPhoto()
                );
        return null;
    }
}
